package admin;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import code.lewdy.app.communication.Alert;
import code.lewdy.app.user.AccountRecord;
import code.lewdy.db.Database;
import code.lewdy.db.HibernateTemple;
import code.lewdy.db.JdbcUtil;
import code.lewdy.util.DataType;
import code.lewdy.util.RandomUtil;
import code.lewdy.web.PassportUtil;
import code.lewdy.web.request.ExtRequest;
import code.lewdy.web.response.ExtResponse;

import com.lt.member.Member;
import com.lt.member.MemberStatistics;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.media.sound.AlawCodec;

/**
 * uri: admin-user-*(Method)
 * 
 * @author liuding,2012-4-25
 * 
 *         $LastChangedDate$ $Author$
 */
public class UserAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {
	AdminService as = new AdminService();
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private ExtRequest ext = null;
	private JdbcUtil jdbc = new JdbcUtil(Database.TYPE_SQLSERVER);

	// params

	private String ext1; // 扩展字段
	private String ext2; //扩展字段2
	private Double number; // 扩展Double类型参数
	
	
	/**检查管理员进入到后台页面中的权限
	 * @return
	 */
	public String checkAdminAccess(){
		System.out.println("check admin access..............");
		int sessionUid = PassportUtil.getUserId(req);
		String results = "0";
		if(as.isAdmin(sessionUid)){
			results = "1";
		}
		ExtResponse.outString(resp, "<data>"+results+"</data>", "xml");
		return null;
	}
	/**
	 * 为会员充值
	 * @return
	 */
	public String recharge() {
		String username = ext1;
		double amount = number;
		List<Member> ms = (List<Member>) jdbc.listAll(Member.class.getName(),
				"select * from Member where username = '" + username + "'");
		if (ms == null || ms.size() == 0) {
			ExtResponse.error(resp, "操作失败：该用户名不存在！", DataType.XML);
			return null;
		}
		Member m = ms.get(0);
		m.setBalance(m.getBalance()+amount);
		HibernateTemple.update(m);
		
		AccountRecord ar = new AccountRecord();
		ar.setAmountIn(amount	);
		ar.setBalance(m.getBalance());
		ar.setCreateDate(new Date());
		ar.setGroupId(AccountRecord.GROUP_RECHARGE_BY_ADMIN);
		ar.setNote(ext2);
		ar.setOrderId(RandomUtil.getRandomString(18));
		ar.setOwnerId(new Long(m.getId()));
		ar.setOwnerName(m.getUsername());
		ar.setType(AccountRecord.INCOME);
		HibernateTemple.save(ar);
		//更新消息通知
		new Alert().save(m, "充值成功提示", "充值"+amount+"已到账，请核对，订单号"+ar.getOrderId()
				+"\n备注："+ext2);
		ExtResponse.ok(resp, "充值成功！", DataType.XML);
		return null;
	}

	/**
	 * 踢会员下线
	 * 
	 * @return
	 * @author liuding,2012-4-25
	 */
	public String kickLogin() {
		int uid = ext.getInt("uid");
		PassportUtil.removeASessionUid(uid);
		return null;
	}

	public void setServletRequest(HttpServletRequest req) {
		// TODO Auto-generated method stub
		this.req = req;
		if (ext == null)
			ext = new ExtRequest(this.req);
	}

	public void setExt1(String ext1) {
		this.ext1 = ext1;
	}

	public String getExt1() {
		return ext1;
	}

	public void setNumber(Double number) {
		this.number = number;
	}

	public Double getNumber() {
		return number;
	}

	public void setServletResponse(HttpServletResponse resp) {
		// TODO Auto-generated method stub
		this.resp = resp;
	}
	public void setExt2(String ext2) {
		this.ext2 = ext2;
	}
	public String getExt2() {
		return ext2;
	}
}
